<?php
namespace app\index\controller;

use app\common\controller\HomeBase;
use Firebase\JWT\JWT;
use org\SendMsg;
use TencentCloud\Captcha\V20190722\CaptchaClient;
use TencentCloud\Captcha\V20190722\Models\DescribeCaptchaResultRequest;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use think\facade\Cache;
use think\facade\Config;
use wechat\WechatAuth;
use app\common\model\User as UserModel;

class Order extends HomeBase
{

    public function create ()
    {
        if($this->request->isPost())
        {
            $actType = trim($this->request->param("actType"));
            $payType = trim($this->request->param("payType"));
            $vipId = (int)$this->request->param("vipId");

            if (empty($this->user_info)) {
                return $this->echoJson("",-1,"请先登录!");
            }

            if ($actType == "vip")
            {
                $vip = model("vip")->where(['id' => $vipId])->find();
                $orderInfo = $this->createOrder($payType,$vip['title'],$vip['price'],$this->user_info['id'],$vip['id']);

                $res = model('order')->insert([
                   'user_id' => $this->user_info['id'],
                   'pay_amount' => $vip['price'],
                   'pay_type' => $payType,
                   'order_id' => $orderInfo['trade_no'],
                   'vip_id' => $vip['id'],
                   'status' => 0, // 0未支付
                   'create_time' => time(), // 订单创建时间
                ]);

                if ($res)
                {
                    return $this->echoJson($orderInfo['qrcode'],200,"订单创建成功");
                }else {
                    return $this->echoJson("",-1,"订单创建失败");
                }
            }else {
                return $this->echoJson("",-1,"订单创建失败");
            }
        }
    }

}
